#include "world.h"

CWorld::CWorld(){
	terrain = new CTerrain(1, 1.0);
}

CWorld::~CWorld(){
/*	audioSystem->Shutdown(); */

	delete terrain;
	delete audioSystem;
	delete worldSound;
	delete gui;
	
}

CWorld::CWorld(CCamera *c){
  camera = c;
  terrain = new CTerrain(35, 1.0f);
  jugador = new CJugador(terrain, 0);
  audioSystem = new CAudioSystem;
  carretera = new CEntity;
  
  gui = new CGUI;
  
  carretera->AttachTo(terrain);
  jugador->AttachTo(terrain);
  
  worldSound = audioSystem->Create("ambient.wav", false);
  audioSystem->Play(worldSound, true);
  
  carretera->Load("models/carretera/tris.md2", "models/carretera/carretera.pcx");
  carretera->position = CVector(500, 10.0, 1100);//50,0,1100
  carretera->direction = 0.0f;
  carretera->size = 0.8;
  carretera->tipo = t_CARRETERA;
  
  // montanyas de la carretera
  mountain1 = new CEntity;
  mountain1->AttachTo(terrain);
  mountain1->Load("models/montana/tris.md2", "models/montana/montana.pcx");
  mountain1->position =  CVector(800.0, 0.0f, 270.0f);	//50,0,150
  mountain1->size = 70;
  mountain1->tipo = t_MONTANA;

  mountain1 = new CEntity;
  mountain1->AttachTo(terrain);
  mountain1->Load("models/montana/tris.md2", "models/montana/montana.pcx");
  mountain1->position = CVector(800.0, 0.0f, 360.0f);	//50,0,150
  mountain1->size = 70; 
  mountain1->tipo = t_MONTANA;

  mountain1 = new CEntity;
  mountain1->AttachTo(terrain);
  mountain1->Load("models/montana/tris.md2", "models/montana/montana.pcx");
  mountain1->position = CVector(920.0, 0.0f, 400.0f);	//50,0,150
  mountain1->size = 70;
  mountain1->tipo = t_MONTANA;
  
  mountain1 = new CEntity;
  mountain1->AttachTo(terrain);
  mountain1->Load("models/montana/tris.md2", "models/montana/montana.pcx");
  mountain1->position = CVector(720.0, 0.0f, 800.0f);	//50,0,150
  mountain1->size = 70;
  mountain1->tipo = t_MONTANA;

  mountain1 = new CEntity;
  mountain1->AttachTo(terrain);
  mountain1->Load("models/montana/tris.md2", "models/montana/montana.pcx");
  mountain1->position = CVector(650.0, 0.0f, 900.0f);	//50,0,150
  mountain1->size = 70;
  mountain1->tipo = t_MONTANA;
 
  mountain1 = new CEntity;
  mountain1->AttachTo(terrain);
  mountain1->Load("models/montana/tris.md2", "models/montana/montana.pcx");
  mountain1->position = CVector(600.0, 0.0f, 1000.0f);	//50,0,150
  mountain1->size = 70;
  mountain1->tipo = t_MONTANA;

  mountain1 = new CEntity;
  mountain1->AttachTo(terrain);
  mountain1->Load("models/montana/tris.md2", "models/montana/montana.pcx");
  mountain1->position = CVector(580.0, 0.0f, 1070.0f);	//50,0,150
  mountain1->size = 70;
  mountain1->tipo = t_MONTANA;

  mountain1 = new CEntity;
  mountain1->AttachTo(terrain);
  mountain1->Load("models/montana/tris.md2", "models/montana/montana.pcx");
  mountain1->position = CVector(670.0, 0.0f, 1400.0f);	//50,0,150
  mountain1->size = 70;
  mountain1->tipo = t_MONTANA;


  jugador->Load("models/coche/tris.md2", "models/coche/macracing.pcx");
  jugador->SetState(MODEL_LEFT);
  jugador->direction = 90.0;
  jugador->position = CVector(670.0, 0.0, 30.0); //670,0,100
  jugador->tipo = t_JUGADOR;

 gui->SetCurrentTime(0);
 gui->SetEnemiesLeft(jugador->nchecks);


  
  CEntity *inicio;
  inicio = new CEntity;
  inicio->size = 1;
  inicio->Load("models/coche/weapon.md2", "models/coche/weapon.PCX");
  inicio->AttachTo(terrain);
  
  ncontrarios = 3;
  srand((unsigned)time(NULL));
  for (int i=1; i<=ncontrarios; i++){
    contrario = new CContrario(i);
    contrario->AttachTo(terrain);
    contrario->setId(i);
    contrario->loadMap(terrain);
	contrario->direction = jugador->direction;
    contrario->position = CVector(680 + i*10, 0.0, 100 + i*10);
  }

  camera->changeView(0);
}

void CWorld::Animate(float deltaTime){
    camera->updateView(jugador);
	terrain->Animate(deltaTime);	
	int checkpoints= jugador->nchecks - jugador->chp;
	if(checkpoints<0){
	   checkpoints=0;
	   gui->DrawWinner();
	   }
	gui->SetEnemiesLeft(checkpoints);
    gui->SetCurrentTime(timeElapsed);
	
    if (!gameDone)
        timeElapsed += deltaTime;
    else
        timeElapsed = timeStart;

}

void CWorld::Draw(CCamera *camera){
	terrain->Draw(camera);
	 gui->Draw();
	 
}

void CWorld::OnPrepare(){
	glClearColor(terrain->fogColor[0], terrain->fogColor[1], terrain->fogColor[2], terrain->fogColor[3]);

	terrain->Prepare();
}
